package com.example.recyclerviewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StaggeredGridLayout extends AppCompatActivity {
    private List<Fruit> fruitList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staggered_grid_layout);
        intiFruits();
        RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler_view3);

        //StaggeredGridLayoutManager传入两个参数，第一个指定布局的列数，第二个指定布局的排列方向
        StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter_StaggeredGridLayout adapter=new FruitAdapter_StaggeredGridLayout(fruitList);
        recyclerView.setAdapter(adapter);
    }

    //初始化水果数据
    private void intiFruits(){
        for(int i=0;i<10;i++){
            Fruit apple=new Fruit(getRandomLengthNmae("Apple"+i),R.drawable.apple_pic);
            fruitList.add(apple);
            Fruit banana=new Fruit(getRandomLengthNmae("Banana"+i),R.drawable.banana_pic);
            fruitList.add(banana);
        }
    }

    private String getRandomLengthNmae(String name) {

        //创造1-20的随机数
        Random random=new Random();
        int length=random.nextInt(20)+1;


        StringBuilder builder=new StringBuilder();
        for (int i=0;i<length;i++){
            builder.append(name);
        }
        return builder.toString();
    }
}
